package main.scala.chapter02

object Test39_wordCount1 {
  def main(args: Array[String]): Unit = {
    val tupleList = List(("Hello Scala Spark World ", 4),
      ("Hello Scala Spark", 3), ("Hello Scala", 2), ("Hello", 1))
    //("Hello",4)(Scala,4)...(Hello,1)
    val tuples = tupleList.map(e => {
      (e._1.split(" ").toList, e._2)
    })
    println(tuples)
    val list = tuples.flatMap(e => {
      e._1.map(f => (f, e._2))
    })
    println(list)
    //按照键值对的键进行分组 将相同词的元组分到1组 。
    val stringToTuples = list.groupBy(e => e._1)
    //统计map中的value里边所有元素的内部和
    println(stringToTuples.mapValues(e=>{e.map(f=>f._2).sum}))
    //把复杂的问题转换成简单问题 将所有的字符串乘以出现的次数 拼接到一起 形成以前的wordcount问题
    val tupleList1 = List(("Hello Scala Spark World ", 4),
      ("Hello Scala Spark", 3), ("Hello Scala", 2), ("Hello", 1))
    val strings = tupleList1.map(e => (e._1 + " ") * e._2)
    println(strings)
//    val str = strings.reduce((x, y) => (x + y))
//    println(str)
    //将所有的单词分开 再将列表压缩成1维列表
    val strings1 = strings.flatMap(e => e.split(" ").toList)
    println(strings1)
    //将空元素去掉
    val strings2 = strings1.filter(e => e!="")
    println(strings2)
    //按照单词分组 再计算数量
    println(strings2.groupBy(e=>e).mapValues(e=>e.size))
    //使用正则表达式
    println(strings.flatMap(e => e.split("\\s+").toList))
  }
}
